package com.gitee.baqkft.code.ui.component;

import com.gitee.baqkft.code.ui.layout.GBC;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
 * Java模型配置
 * @Author: chenmingcai
 * @Date 2020/5/30 1:41 下午
 **/

public class JavaModelConfigPanel extends JPanel
{
    /**
     * 目标包
     */
    private final JTextField targetPkg;

    /**
     * 目标工程
     */
    private final JTextField targetProject;

    /**
     * 允许子包
     */
    private final JCheckBox subPkg;

    public JavaModelConfigPanel()
    {
        setLayout(new GridBagLayout());
        JLabel label = new JLabel("TargetPackage:");
        GBC gbc = new GBC(0, 0, 1, 1);
        gbc.setWeight(0, 1).setFill(GBC.VERTICAL);
        add(label, gbc);

        targetPkg = new JTextField("default");
        gbc = new GBC(1, 0, 1, 1);
        gbc.setWeight(1, 1).setFill(GBC.BOTH);
        add(targetPkg, gbc);

        label = new JLabel("TargetProject:");
        gbc = new GBC(0, 1, 1, 1);
        gbc.setWeight(0, 1).setFill(GBC.VERTICAL);
        add(label, gbc);

        targetProject = new JTextField("src/main/java");
        gbc = new GBC(1, 1, 1, 1);
        gbc.setWeight(1, 1).setFill(GBC.BOTH);
        add(targetProject, gbc);

        JButton browse = new JButton("选择");
        gbc = new GBC(2, 1, 1, 1);
        gbc.setWeight(0, 1).setFill(GBC.VERTICAL);
        add(browse, gbc);

        browse.addActionListener(e->{
            chooseTargetProject();
        });

        subPkg = new JCheckBox("允许子包");
        gbc = new GBC(2, 0, 1, 1);
        gbc.setWeight(0, 1).setFill(GBC.VERTICAL);
        add(subPkg, gbc);
    }

    /**
     * 选择目标位置
     */
    private void chooseTargetProject()
    {
        JFileChooser chooser = new JFileChooser();
        File file = new File(targetProject.getText().trim());
        if(file.exists())
        {
            chooser = new JFileChooser(targetProject.getText());
        }

        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if(JFileChooser.APPROVE_OPTION == chooser.showDialog(this, "选择"))
        {
            String dir = chooser.getSelectedFile().getPath();
            file = new File(dir);
            if(!file.exists())
            {
                int idx = dir.lastIndexOf(File.separator);
                dir = dir.substring(0, idx);
            }
            targetProject.setText(dir);
        }
    }

    /**
     * 获取目标包
     * @return
     */
    public String getTargetPackage()
    {
        return targetPkg.getText().trim();
    }

    /**
     * 获取目标工程
     * @return
     */
    public String getTargetProject()
    {
        return targetProject.getText().trim();
    }

    /**
     * 获取允许子包
     * @return
     */
    public boolean isEnableSubPackage()
    {
        return subPkg.isSelected();
    }
}
